/**
 * Created by zhanglihe on 15/11/7.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog',
    'datepicker'
], function( $, common, ready, Collection, template, Model, dialog ,datepicker) {
    var index = function() {
        this.init();
    };
    index.prototype = {
        init: function() {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function() {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'apply_act',
                model: {
                    url:_global.url.api + 'apply_act',
                    event: {
                        '.item-del:click': 'remove',
                        '.item-edit:click':'edit'
                    }
                    ,edit:function(e){
                        e.preventDefault();
                        e.preventDefault();
                        _this.data = this.data;
                        var model = this;
                        e.preventDefault();
                        _this.dialog = dialog({
                            title: '修改活动',
                            content: template('add-tpl', this.data),
                            width: 600,
                            ok: function () {
                                _this.data.position=$("#position").val();
                                if(_this.data.position==0){
                                    Alert({msg:'请选择投放的位置',type:'error'});
                                    return false;
                                }
                                _this.data.img = $("input[name='imgs']").val();
                                if(_this.data.img==''||_this.data.img==undefined){
                                    Alert({msg:'请传入一张图片',type:'error'});
                                    return false;
                                }
                                _this.data.start_time =$("#dt_start").val();
                                if(_this.data.start_time==''){
                                    Alert({msg:'请输入开始日期',type:'error'});
                                    return false;
                                }
                                _this.data.end_time =$("#dt_end").val();
                                if(_this.data.end_time==''){
                                    Alert({msg:'请输入结束日期',type:'error'});
                                    return false;
                                }
                                _this.data.listorder =$("#listorder").val();
                                _this.data.id = _this.data.id;
                                if (model.put()) {
                                    _this.collection.fetch();
                                }
                                ;
                            }, cancel: function () {
                            }
                        });
                        _this.dialog.show();
                    },
                    remove: function(e) {
                        e.preventDefault();
                        var model = this;
                        dialog({
                            'content': '确定删除吗?',
                            ok: function() {
                                if(model.del()){
                                    _this.collection.fetch();
                                };
                            }, cancel: function() {}
                        }).show(e.target);
                    }
                },
                callback: function() {
                    loaded();
                }
            });
            this.collection.fetch();
        },

        event: function() {
            this.data = {};
            var _this = this;
            //保存
            $('body').on('click','.long_date', function() {
                WdatePicker({
                    el:this.id,
                    dateFmt:'yyyy-MM-dd HH:mm:ss'
                });
            });
            $('body').on('click', '#btn-upload', function(e) {
                _this.upload_dialog = dialog({
                    content: template('upload-tpl', {}),
                    width: 550,
                    ok: function() {
                        var img = $("#upload-frame")[0].contentWindow.image.get();
                        _this.imgPreview(img);
                    }, cancel: function() {}
                });
                _this.upload_dialog.show();
            });
        },

        change: function(e) {
            this.data[$(e.target).attr('name')] = $(e.target).val();
        },

        imgPreview: function(img) {
            console.log('this.data',this.data);
            if(typeof img.id !== 'undefined') {
                this.data.img = img.id;
                $('#img-view').html(template('img-tpl', img));
            }
        }

    };

    new index();

});